package com.icezzz.maven_gen;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;

public class MavenGen {
    public static final String nexus = "aliyun";// central

    public static void gen(String folder) {
        File folderFile = new File(folder);
        if (folderFile.isDirectory()) {
            gen(folderFile);
        }
    }

    private static void gen(File folder) {
        File[] listFiles = folder.listFiles();
        String baseFileName = null;
        String filePath = null;
        List<String> nexusFileList = new ArrayList<>();
        for (File file : listFiles) {
            String absolutePath = file.getAbsolutePath();
            absolutePath = absolutePath.replace("\\", "/");
            String suffix = FileNameUtil.getSuffix(file);
            String fileName = FileNameUtil.getName(file);
            if (filePath == null) {
                filePath = StrUtil.subBefore(absolutePath, "/", true);
            }
            if (isSignFile(fileName, suffix)) {
                String sha1 = SecureUtil.sha1(file);
                String sha1FileName = absolutePath + ".sha1";
                if (!FileUtil.exist(sha1FileName)) {
                    FileUtil.writeUtf8String(sha1, sha1FileName);
                }
                if (fileName.endsWith(".jar") && !fileName.endsWith("-sources.jar")
                        && !fileName.endsWith("-javadoc.jar")) {
                    baseFileName = StrUtil.removeSuffix(fileName, ".jar");
                }
                nexusFileList.add(fileName);
            }
        }
        if (baseFileName != null) {
            Date date = new Date();
            System.out.println("path = " + filePath);
            String CONST_REMOTE_REPOSITORIES = "_remote.repositories";
            String CONST_M2E_LAST_UPDATED = "m2e-lastUpdated.properties";
            String remoteRepositoriesPath = filePath + "/" + CONST_REMOTE_REPOSITORIES;
            String m2eLastUpdatedPath = filePath + "/" + CONST_M2E_LAST_UPDATED;
            if (!FileUtil.exist(remoteRepositoriesPath)) {
                String content = genRemoteRepositories(nexusFileList, date);
                FileUtil.writeUtf8String(content, remoteRepositoriesPath);
            }
            if (!FileUtil.exist(m2eLastUpdatedPath)) {
                String content = genM2eLastUpdated(nexusFileList, date);
                FileUtil.writeUtf8String(content, m2eLastUpdatedPath);
            }
        }
    }

    private static String genRemoteRepositories(List<String> nexusFileList, Date date) {
        String nl = "\r\n";
        StringBuilder sb = new StringBuilder();
        sb.append(
                "#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.");
        sb.append(nl);
        sb.append("#");
        sb.append(date);
        sb.append(nl);
        for (String nexusFileName : nexusFileList) {
            sb.append(nexusFileName);
            if ("aliyun".equals(nexus)) {
                sb.append(">nexus-${nexus}=".replace("${nexus}", nexus));
            } else {
                sb.append(">central=");
            }
            sb.append(nl);
        }
        return sb.toString();
    }

    private static String genM2eLastUpdated(List<String> nexusFileList, Date date) {
        String nl = "\r\n";
        StringBuilder sb = new StringBuilder();
        long time = date.getTime();
        sb.append("#");
        sb.append(date);
        sb.append(nl);
        if ("aliyun".equals(nexus)) {
            sb.append("nexus-${nexus}".replace("${nexus}", nexus));
        } else {
            sb.append("central");
        }
        sb.append("|");
        if ("aliyun".equals(nexus)) {
            sb.append("http\\://maven.aliyun.com/nexus/content/groups/public");
        } else {
            sb.append("http\\://repo.maven.apache.org/maven2");
        }
        sb.append("|");
        sb.append("sources=");
        sb.append(time);
        sb.append(nl);
        return sb.toString();
    }

    private static boolean isSignFile(String fileName, String suffix) {
        if (StrUtil.isBlank(fileName)) {
            return false;
        }
        if ("_remote.repositories".equals(fileName)) {
            return false;
        }
        if ("m2e-lastUpdated.properties".equals(fileName)) {
            return false;
        }
        if (fileName.endsWith(".jar.lastUpdated")) {
            return false;
        }
        if (fileName.endsWith(".pom.lastUpdated")) {
            return false;
        }
        if (fileName.endsWith(".jar.sha1")) {
            return false;
        }
        if (fileName.endsWith(".pom.sha1")) {
            return false;
        }
        if (fileName.endsWith(".jar")) {
            return true;
        }
        if (fileName.endsWith(".pom")) {
            return true;
        }
        return false;
    }

}
